﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class Files : BaseBAL<FilesInfo, Models.Files>
    {
        /// <summary>
        /// 得到所有信息
        /// </summary>
        /// <param name="first">从第几条开始读</param>
        /// <param name="max">读几条</param>
        /// <returns></returns>
        public static IList<FilesInfo> GetList(int first, int max)
        {
            return GetList(0, "", 0, first, max);
        }

        /// <summary>
        /// 统计数量
        /// </summary>
        /// <param name="subjectid">知识点ID</param>
        /// <param name="keyword">关键字</param>
        /// <param name="userid">用户ID</param>
        /// <returns></returns>
        public static int GetCount(int subjectid, string keyword, int userid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.Files, bool>>> funcs = new List<Expression<Func<Models.Files, bool>>>();
                if (subjectid > 0)
                {
                    funcs.Add(t => t.Subject == subjectid);
                }
                if (userid > 0)
                {
                    funcs.Add(t => t.UserId == userid || t.UserId == 0);
                }
                if (keyword != null && keyword != "")
                {
                    funcs.Add(t => t.Name.Contains(keyword));
                }
                return GetT2IQueryable(funcs, db).Count();
            }
        }

        /// <summary>
        /// 得到文件列表
        /// </summary>
        /// <param name="subjectid">课程ID</param>
        /// <param name="keyword">关键字</param>
        /// <param name="userid">用户ID</param>
        /// <param name="first">从第几条开始读取</param>
        /// <param name="max">读取几条</param>
        /// <returns></returns>
        public static IList<FilesInfo> GetList(int subjectid, string keyword, int userid, int first, int max)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                IList<Expression<Func<Models.Files, bool>>> funcs = new List<Expression<Func<Models.Files, bool>>>();
                if (subjectid > 0)
                {
                    funcs.Add(t => t.Subject == subjectid);
                }
                if (userid > 0)
                {
                    funcs.Add(t => t.UserId == userid || t.UserId == 0);
                }
                if (keyword != null && keyword != "")
                {
                    funcs.Add(t => t.Name.Contains(keyword));
                }
                if (max == 0)
                {
                    return GetT2IQueryable(funcs, db).OrderByDescending(t => t.Id).Select(t => TransT2ToT1(t)).ToList();
                }
                else
                {
                    return GetT2IQueryable(funcs, db).OrderByDescending(t => t.Id).Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                }
            }
        }

        public static IList<FilesInfo> GetList(int first, int max, int subjectid,string keyword)
        {
            
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (max > 0)
                {
                    if (subjectid > 0)
                    {
                        return db.GetTable<Models.Files>().Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                    }
                    else if (keyword != "")
                    {
                        return db.GetTable<Models.Files>().Where(t => t.Name.Contains(keyword)).Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                    }
                    else
                    {
                        return db.GetTable<Models.Files>().Skip(first).Take(max).Select(t => TransT2ToT1(t)).ToList();
                    }
                }
                else
                {
                    if (subjectid > 0)
                    {
                        return db.GetTable<Models.Files>().Where(t => t.Subject == subjectid).Select(t => TransT2ToT1(t)).ToList();
                    }
                    else if (keyword != "")
                    {
                        return db.GetTable<Models.Files>().Where(t => t.Name.Contains(keyword)).Select(t => TransT2ToT1(t)).ToList();
                    }
                    else
                    {
                        return db.GetTable<Models.Files>().Select(t => TransT2ToT1(t)).ToList();
                    }
                }
            }
        }

        public static int GetCount(int subjectid, string keyword)
        {
            using(var db=DatabaseHelper.GetDashboardData())
            {
                if (subjectid > 0)
                {
                    return db.GetTable<Models.Files>().Where(t => t.Subject == subjectid).Count();
                }
                else if(keyword!="")
                {
                    return db.GetTable<Models.Files>().Where(t => t.Name.Contains(keyword)).Count();
                }
                else
                {
                    return GetT1Count();
                }
            }
        }
    }
}
